arrispwgen_test.js ➔ test_single_passwords   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 2
dl 0
loc 12
rs 9.4285
nop 3

1 Function

Rating   Name   Duplication   Size   Complexity  
A arrispwgen_test.js ➔ ... ➔ assert.test 0 4 1
1
import tape from 'tape';
2
import { potd_using_default_seed, potd_using_custom_seed, custom_seed } from './helper_data';
3
import * as a from '../lib/arrispwgen';
4
5
function simple_date(date) {
6
    return date.toLocaleDateString('en-GB', {day: '2-digit', month: '2-digit', year: 'numeric'});
7
}
8
9
// DRY function for testing single password
10
function test_single_passwords(assert, data, seed) {
11
    for (let p in data) {
12
        let date = (new Date(parseInt(p)));
13
        let potd = a.generate(date, seed);
14
        assert.test('Password for: ' + simple_date(date) + ' (timestamp: ' + p + ')', function (test) {
15
            test.equal(potd, data[p], 'Generated password should be correct.');
16
            test.end();
17
        });
18
    }
19
20
    assert.end();
21
}
22
23
tape('Should generate the correct password for the given days with the default seed', function(assert) {
24
    test_single_passwords(assert, potd_using_default_seed);
25
});
26
27
tape('Should generate the correct password for the given days with a custom seed', function (assert) {
28
    test_single_passwords(assert, potd_using_custom_seed, custom_seed);
29
});
30
31
32
tape('Should throw an exception if start date is after end date', function (assert) {
33
    assert.plan(1);
34
    let fn = function () {
35
        let d1 = new Date(2016, 1, 10);
36
        let d2 = new Date(2016, 1, 5);
37
        a.generate_multi(d1, d2);
38
    };
39
40
    assert.throws(fn);
41
});
42
43
tape('Should generate a single password if the date interval is just one day', function (assert) {
44
    assert.plan(1);
45
    let d1 = new Date(2016, 1, 5);
46
    let d2 = new Date(2016, 1, 5);
47
    let potd = a.generate_multi(d1, d2);
48
49
    assert.equal(Object.keys(potd).length, 1, 'Should generate 1 password');
50
});
51
52
function test_multiple_passwords(assert, data, start_index, end_index, seed) {
53
    let keys = Object.keys(data);
54
55
    let start = new Date(parseInt(keys[start_index]));
56
    let end = new Date(parseInt(keys[end_index]));
57
    let potd = a.generate_multi(start, end, seed);
58
    let count = end_index - start_index + 1;
59
    assert.equal(Object.keys(potd).length, count, 'Should generate ' + count + ' passwords');
60
    for (let p in potd) {
61
        let date = simple_date((new Date(parseInt(p))));
62
        assert.test('Password for: ' + date + ' (timestamp: ' + p + ')', function (test) {
63
            test.equal(potd[p], data[p], 'Generated password should be correct.');
64
            test.end();
65
        });
66
    }
67
}
68
69
tape('Should generate the correct passwords for the given date interval, with the default seed', function (assert) {
70
    test_multiple_passwords(assert, potd_using_default_seed, 0, 3);
71
    test_multiple_passwords(assert, potd_using_default_seed, 4, 6);
72
    assert.end();
73
});
74
75
tape('Should generate the correct passwords for the given date interval, with a custom seed', function (assert) {
76
    test_multiple_passwords(assert, potd_using_custom_seed, 0, 3, custom_seed);
77
    test_multiple_passwords(assert, potd_using_custom_seed, 4, 6, custom_seed);
78
    assert.end();
79
});
80